import { createPinia } from "pinia";
import { createPersistedState } from "pinia-plugin-persistedstate";

// 定义 UniApp 存储适配器的类型
interface StorageAdapter {
  getItem: (key: string) => string | null;
  setItem: (key: string, value: string) => void;
}

//小程序没有localStorage API，所以我们需要自定义存储
const uniStorage: StorageAdapter = {
  getItem(key: string) {
    try {
      return uni.getStorageSync(key) || null;
    } catch (error) {
      console.error("读取存储失败：", error);
      return null;
    }
  },
  setItem(key: string, value: string) {
    try {
      uni.setStorageSync(key, value);
    } catch (error) {
      console.error("写入存储失败：", error);
    }
  },
};

// 初始化 Pinia 并配置持久化插件
export const pinia = createPinia().use(
  createPersistedState({
    storage: uniStorage,
  })
);

// 统一导出所有 Store
export * from "./modules/page";
export * from "./modules/message";
export * from "./modules/user";
export * from "./modules/medical";
